#include "marsspace.h"
#include <QGlobalStatic>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>

Q_GLOBAL_STATIC(MarsSpace,m_instance)

MarsSpace::MarsSpace()
{
    initDialog();
}

MarsSpace *MarsSpace::Instance()
{
    return m_instance;
}

void MarsSpace::SetCurrentData(qreal space_left, qreal space_top, qreal space_right, qreal space_bottom)
{
    lineedit_left->setText(QString::number(space_left,'f',2));
    lineedit_top->setText(QString::number(space_top,'f',2));
    lineedit_right->setText(QString::number(space_right,'f',2));
    lineedit_bottom->setText(QString::number(space_bottom,'f',2));
}

void MarsSpace::ChangeCurrentData(qreal &space_left, qreal &space_top, qreal &space_right, qreal &space_bottom)
{
    space_left = lineedit_left->text().toDouble();
    space_top = lineedit_top->text().toDouble();
    space_right = lineedit_right->text().toDouble();
    space_bottom = lineedit_bottom->text().toDouble();
}

void MarsSpace::initDialog()
{
    btn_ok = new QPushButton("确认");
    connect(btn_ok,&QPushButton::clicked,this,[=](){
        accept();
    });
    btn_cancle = new QPushButton("取消");
    connect(btn_cancle,&QPushButton::clicked,this,[=](){
       reject();
    });

    h_layout_btn = new QHBoxLayout();
    h_layout_btn->addWidget(btn_ok);
    h_layout_btn->addWidget(btn_cancle);

    h_layout_left = new QHBoxLayout();
    label_left = new QLabel("左侧边距：",this);
    lineedit_left = new QLineEdit(this);
    h_layout_left->addWidget(label_left);
    h_layout_left->addSpacing(10);
    h_layout_left->addWidget(lineedit_left);

    h_layout_top = new QHBoxLayout();
    label_top = new QLabel("顶部边距：",this);
    lineedit_top = new QLineEdit(this);
    h_layout_top->addWidget(label_top);
    h_layout_top->addSpacing(10);
    h_layout_top->addWidget(lineedit_top);

    h_layout_right = new QHBoxLayout();
    label_right = new QLabel("右侧边距：",this);
    lineedit_right = new QLineEdit(this);
    h_layout_right->addWidget(label_right);
    h_layout_right->addSpacing(10);
    h_layout_right->addWidget(lineedit_right);

    h_layout_bottom = new QHBoxLayout();
    label_bottom = new QLabel("底部边距：",this);
    lineedit_bottom = new QLineEdit(this);
    h_layout_bottom->addWidget(label_bottom);
    h_layout_bottom->addSpacing(10);
    h_layout_bottom->addWidget(lineedit_bottom);

    v_layout = new QVBoxLayout(this);
    v_layout->addLayout(h_layout_left);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_top);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_right);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_bottom);
    v_layout->addSpacing(10);
    v_layout->addLayout(h_layout_btn);
    v_layout->addSpacing(10);

    setLayout(v_layout);

    resize(300,400);
}
